package com.mall.action;

import com.mall.bean.Collection;
import com.mall.service.CollectionService;
import com.mall.uitl.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-25 14 39
 * discription
 */
@RestController
@RequestMapping("/Index/CollectionController")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @RequestMapping("getCollectionByMemberId/{memberId}")
    public JsonUtil getCollectionByMemberId(@PathVariable("memberId") String memberId) {
        List<Collection> list = collectionService.getCollectionByMemberId(memberId);
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(list);
        return jsonUtil;
    }

    //collectionGoodsById
    @RequestMapping("collectionGoodsById")
    public JsonUtil collectionGoodsById(String goodsId, String memberId) {

        Collection collectionExist = collectionService.getCollectionByMemberIdAndGoodsId(goodsId, memberId);

        JsonUtil jsonUtil = new JsonUtil();

        if (collectionExist != null) {
            //该商品已被该会员收藏过
            jsonUtil.setMsg("该商品已经被收藏过,亲");
            jsonUtil.setCode(1);
        } else {
            int res = collectionService.collectionGoodsById(goodsId, memberId);
            jsonUtil.setMsg("收藏成功");
        }
        return jsonUtil;
    }

    //removeFavoriteById
    @RequestMapping("removeFavoriteById/{itemId}")
    public JsonUtil removeFavoriteById(@PathVariable("itemId") String itemId) {

        JsonUtil jsonUtil = new JsonUtil();
        int  res = collectionService.removeFavoriteById(itemId);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }
    //removeFavoritemArrayById
    @RequestMapping("removeFavoritemArrayById")
    public JsonUtil removeFavoritemArrayById(@RequestParam("idArrayStr") String idArrayStr) {
        JsonUtil jsonUtil = new JsonUtil();
        String regex = ",";
        String[] idArray= idArrayStr.split(regex);
        int res = collectionService.removeFavoritemArrayById(idArray);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }
}
